#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义审批人员结构体
typedef struct Approver {
    char name[50];
    int approvalLimit;  // 该审批人员的审批额度
    struct Approver* next;  // 下一个审批人员
} Approver;

// 创建新的审批人员
Approver* createApprover(const char* name, int limit, Approver* next) {
    Approver* approver = (Approver*)malloc(sizeof(Approver));
    if (approver == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(1);
    }
    strcpy(approver->name, name);
    approver->approvalLimit = limit;
    approver->next = next;
    return approver;
}

// 处理报销请求
void handleRequest(Approver* approver, int amount) {
    if (approver == NULL) {
        printf("No approver available to handle the request.\n");
        return;
    }

    if (amount <= approver->approvalLimit) {
        printf("%s approved the request for $%d\n", approver->name, amount);
    } else if (approver->next != NULL) {
        printf("%s cannot approve $%d, forwarding to next approver...\n", approver->name, amount);
        handleRequest(approver->next, amount);
    } else {
        printf("Request of $%d could not be approved by anyone.\n", amount);
    }
}

// 释放审批人员链表的内存
void freeApprovers(Approver* approver) {
    while (approver != NULL) {
        Approver* temp = approver;
        approver = approver->next;
        free(temp);
    }
}

int main() {
    // 创建审批人员
	Approver* vicePresident = createApprover("Vice President", 20000,NULL);
	Approver* director = createApprover("Director", 10000,vicePresident);
    Approver* manager = createApprover("Manager", 5000,director);

    // 处理报销请求
    int amounts[] = {3000, 6000, 12000, 25000};
    for (int i = 0; i < sizeof(amounts) / sizeof(amounts[0]); i++) {
        printf("\nHandling request for $%d:\n", amounts[i]);
        handleRequest(manager, amounts[i]);
    }

    // 释放内存
    freeApprovers(manager);

    return 0;
}
